package com.mew.user.controller;

import com.mew.common.entity.user.Profile;
import com.mew.common.entity.user.User;
import com.mew.common.exception.ResponseCode;
import com.mew.common.rest.user.UserDetailedInfoResponseMessage;
import com.mew.common.rest.user.VipInfoMessage;
import com.mew.user.service.UserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author dylin
 * @create 2021/1/27 0027
 */
@Controller
@RequestMapping("/user")
@Slf4j
public class UserController {
    @Autowired
    private UserInfoService userInfoService;

    @PostMapping("/update")
    @ResponseBody
    public int updateUser(@RequestBody User user){
        userInfoService.updateByID(user);
        return ResponseCode.Success.code();
    }

    @PostMapping("/user/update_nickname")
    @ResponseBody
    public int updateNickname(@RequestParam("uid")long uid,@RequestParam("telephone") String nickname){
        return userInfoService.updateNickname(User.builder().id(uid).nickname(nickname).build());
    }

    @PostMapping("/update_account")
    @ResponseBody
    public int updateAccount(@RequestBody User user){
        return userInfoService.updateAccount(user);
    }


    @PostMapping("/updateprofile")
    @ResponseBody
    public int updateUser(@RequestBody Profile profile){
        try {
            userInfoService.updateProfileByid(profile);
            return ResponseCode.Success.code();
        } catch (Exception e) {
            return ResponseCode.SysException.code();
        }
    }

    @PostMapping("/bind_telephone")
    @ResponseBody
    int bindTelephone(@RequestParam("uid")long uid,@RequestParam("telephone") String telephone){
        return userInfoService.bindTelephone(uid,telephone);
    }

    /**
     * 获取详细信息 通过uid
     */
    @PostMapping("/detailed_by_uid")
    @ResponseBody
    public UserDetailedInfoResponseMessage getDetailedInfoByUid(@RequestParam("uid")long uid,@RequestParam("qid") long qid){
        return userInfoService.getDetailedInfo(uid,qid);
    }

    /**
     * 获取详细信息 通过gid
     */
    @PostMapping("/detailed_by_gid")
    @ResponseBody
    public UserDetailedInfoResponseMessage getDetailedInfoByGid(@RequestParam("uid")long uid,@RequestParam("gid")String gid){
        return userInfoService.getDetailedInfo(uid,gid);
    }

    /**
     * 获取详细信息 通过gid
     */
    @GetMapping("/vip/{uid}")
    @ResponseBody
    public VipInfoMessage vip(@PathVariable("uid")long uid){
        try {
            VipInfoMessage vip = userInfoService.vip(uid);
            //前端不要vip0
            List<VipInfoMessage.Level> levels = vip.getLevels();
            for (int i = 0; i < levels.size(); i++) {
                VipInfoMessage.Level level = levels.get(0);
                if (level.getVip()==0){
                    levels.remove(level);
                    break;
                }
            }
            return vip;
        } catch (Exception e) {
           log.error(e.getMessage(), e);
        }

        return new VipInfoMessage();
    }

    @GetMapping("/guide/{uid}/{t}")
    @ResponseBody
    public Integer guide(@PathVariable("uid")long uid, @PathVariable("t")int t){

        try {
            userInfoService.guide(uid, t);
            return 1;
        }catch (Exception e){
            log.error(e.getMessage(), e);
            return 0;
        }
    }
}
